package com.lrkj.back.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.entity.TbWenjuan;
import com.lrkj.back.entity.TbWenjuanHuida;
import com.lrkj.back.service.TbDepartmentService;
import com.lrkj.back.service.TbWenjuanAnswerService;
import com.lrkj.back.service.TbWenjuanService;
import com.lrkj.back.Const;
import com.lrkj.back.entity.TbDepartment;
import com.lrkj.back.entity.TbWenjuanAnswer;
import com.lrkj.back.service.TbWenjuanHuidaService;
import com.lrkj.back.service.TbWenjuanQuestionService;
import com.lrkj.back.vo.WenjuanTongjiVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lrkj.back.entity.TbWenjuanQuestion;
import cn.com.cbons.mamisay.parent.exception.BusinessException;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/wenjuan")
public class TbWenjuanController extends BaseController {
	@Autowired
	private TbWenjuanService wenjuanService;
	@Autowired
	private TbWenjuanQuestionService questionService;
	@Autowired
	private TbWenjuanAnswerService answerService;
	@Autowired
	private TbWenjuanHuidaService huidaService;
	@Autowired
	private TbDepartmentService tbDepartmentServices;
	//问卷展示
	@RequestMapping("/wenjuanList.do")
	public ModelAndView wenjuanList(String searchTxt,
			HttpServletRequest request, HttpServletResponse response) throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex") == null ? 1
				: Integer.valueOf(request.getParameter("pageIndex"));
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(searchTxt)) {
			map.put("dabaoName", searchTxt);
		}
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<TbWenjuan> page = wenjuanService.findAllWenjuan(map);
		request.setAttribute("wenjuanList", page.getList());
		request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
		request.setAttribute("total", page.getPages());
		return new ModelAndView("/linchuang/wenjuan/wenjuan_list");
	}
	//数据统计
	//public modelandview yijianedit integer id httpservletrequest
	//list questionlist = questionservice selectbywjid
	@RequestMapping("/wenjuanView.do")
	public ModelAndView yijianEdit(Integer id, HttpServletRequest request,
			HttpServletResponse response) throws BusinessException{
		TbWenjuan wenjuan = (id == null) ? new TbWenjuan() : wenjuanService
				.selectByPrimaryKey(id);
		List<TbWenjuanQuestion> questionList = questionService.selectByWjId(id);
		for (TbWenjuanQuestion question : questionList) {
			List<TbWenjuanAnswer> answerList = answerService
					.selectByQuestionId(question.getId());
			question.setAnswerList(answerList);
		}
		wenjuan.setQuestions(questionList);
		request.setAttribute("wenjuan", wenjuan);
		
		if(id!=null){
			int wenjuanid =id;
			//根据id 查询所以 选题表中所以题  list
			//list<tbwenjuanQuestion questionlist = questionservice.selectbywjid
			//titang 10nm++ ++mn01
			List<TbWenjuanQuestion> questionlist =questionService.selectByWjId(wenjuanid);
			//循环一 获取每题
			List<WenjuanTongjiVo> list = null;
			Map<Integer,List<WenjuanTongjiVo>> resultMap = new HashMap<Integer,List<WenjuanTongjiVo>>();
			for(TbWenjuanQuestion question : questionlist){
				 System.out.println(question);
				 list= new ArrayList<WenjuanTongjiVo>();
				 for(TbWenjuanAnswer answer: question.getAnswerList()){
					 //查询每个的统计次数
					 TbWenjuanHuida emp = new TbWenjuanHuida();
			 		 emp.setWenjuanId(wenjuanid);
				 	 emp.setQuestionId(question.getId());
			 		 emp.setAnswer(answer.getLabel());
					 int count=huidaService.selectcountWenjuan(emp);    //统计次数
					 WenjuanTongjiVo vo = new WenjuanTongjiVo();
					 vo.setId(answer.getId());
					 vo.setQuestionId(question.getId());
					 vo.setLabelVal(answer.getLabelVal());
					 vo.setLabelCount(count);
					 list.add(vo);
				 }
				 resultMap.put(question.getId(), list);
				 System.out.println("resultMap:"+JSON.toJSON(resultMap));
			}
			
			request.setAttribute("resultMap", resultMap);
			
		}
		return new ModelAndView("/linchuang/wenjuan/wenjuan_view");
	}
	// 问卷所以题展示
	@RequestMapping("/wenjuanDaTi.do")
	public ModelAndView wenjuanDaTi(Integer id, HttpServletRequest request,
			HttpServletResponse response) throws BusinessException{
		TbWenjuan wenjuan = (id == null) ? new TbWenjuan() : wenjuanService
				.selectByPrimaryKey(id);
		List<TbWenjuanQuestion> questionList = questionService.selectByWjId(id);
		for (TbWenjuanQuestion question : questionList) {
			List<TbWenjuanAnswer> answerList = answerService
					.selectByQuestionId(question.getId());
			question.setAnswerList(answerList);
		}
		wenjuan.setQuestions(questionList);
		request.setAttribute("wenjuan", wenjuan);
		System.out.println(JSON.toJSON(wenjuan));
				
		
		return new ModelAndView("/linchuang/wenjuan/wenjuan_da");
	}
	//数据增加  异步
	@RequestMapping("/wenjuanSave.do")
	@ResponseBody
	public Map<Object,Object> wenjuanSave(HttpServletRequest request,
			HttpServletResponse response) throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object, Object>();
		try {
			TbWenjuanHuida huida = new TbWenjuanHuida();
			String id = request.getParameter("id");
			huida.setWenjuanId(Integer.valueOf(id));
			String[] questionIds = request.getParameterValues("questionId");
			for (String questionId : questionIds) {
				String[] val = request.getParameterValues("answers"
						+ questionId);
				String comp = StringUtils.join(val, ",");
				huida.setQuestionId(Integer.valueOf(questionId));
				huida.setAnswer(comp);
				huida.setRecordTime(new Date());
				huidaService.insert(huida);
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	@RequestMapping("/wenjuanDel.do")
	@ResponseBody
	public Map<Object,Object> afficheDel(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		try {
			String ids = request.getParameter("id");
			String[] arraId = ids.split(",");
			int sid =0;
			for(String tid : arraId){
				sid =Integer.parseInt(tid);
				huidaService.deletebywenjuanid(sid);
				answerService.deletebywenjuanid(sid);
				questionService.deletebywenjuanid(sid);		
				wenjuanService.deleteByPrimaryKey(sid);
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "删除成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "删除失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	
	
	//下拉框的显示中心  科室  
	@RequestMapping("/wenjuanEdit.do")
	public ModelAndView wenjuanEdit(String searchTxt,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{		
		
		//查询所以科室
		List<TbDepartment> keshilist = tbDepartmentServices.findAllDepartment();
		request.setAttribute("keshilist", keshilist);	
		return new ModelAndView("linchuang/wenjuan/wenjuan_edit");
	}
	
	// 问卷 增加
	@RequestMapping("/wenjuanform.do")
	@ResponseBody
	public Map<Object,Object> wenjuanform(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		try {
			String wenjuanName =request.getParameter("wenjuanName");  //问卷名称
			String wenjuanKeshi =request.getParameter("wenjuanKeshi");//科室id
				
			String[] strQuestion = request.getParameterValues("question");
			String[] strDatetype = request.getParameterValues("datiType");
			String[] strQuestion1= request.getParameterValues("question1");
			String[] strQuestion2 = request.getParameterValues("question2");
			String[] strQuestion3 = request.getParameterValues("question3");
			String[] strQuestion4 = request.getParameterValues("question4");
			
			Date date = new Date();
		
				//增加 问卷表
				TbWenjuan stWenjuan = new TbWenjuan();
				stWenjuan .setWenjuanName(wenjuanName);
				   // 科室id		
				 stWenjuan.setWenjuanKeshi(Integer.parseInt(wenjuanKeshi));		
				 stWenjuan.setWenjuanCreatetime(date);
				 wenjuanService.insert(stWenjuan);
				 int wenjuanId =stWenjuan.getId();
				
				 for(int j =0;j<strQuestion.length;j++){
						//增加题目表
						TbWenjuanQuestion stQuestion = new TbWenjuanQuestion();
						stQuestion.setQuestion(strQuestion[j]);
						stQuestion.setDatiType(Integer.parseInt(strDatetype[j]));
						stQuestion.setWenjuanId(wenjuanId); //插入 问卷id
						stQuestion.setRecordtime(date);
						TbWenjuanQuestion stQue =questionService.insert(stQuestion); //题增加
						 int questionId = stQue.getId(); //题id					
						//增加项目表
						int xx =1;
						if(StringUtils.isNotBlank(strQuestion1[j])){
							TbWenjuanAnswer answer = new TbWenjuanAnswer();
							answer.setWenjuanId(wenjuanId);
							answer.setQuestionId(questionId);
							answer.setLabel(xx+"");
							xx++;
							answer.setLabelVal(strQuestion1[j]);
							answer.setRecordTime(date);
							answerService.insertSelective(answer);
						}
						if(StringUtils.isNotBlank(strQuestion2[j])){
							TbWenjuanAnswer answer2 = new TbWenjuanAnswer();
							answer2.setWenjuanId(wenjuanId);
							answer2.setQuestionId(questionId);
							answer2.setLabel(xx+"");	
							xx++;
							answer2.setLabelVal(strQuestion2[j]);
							answer2.setRecordTime(date);
							answerService.insertSelective(answer2);
						}
						if(StringUtils.isNotBlank(strQuestion3[j])){
							TbWenjuanAnswer answer3 = new TbWenjuanAnswer();
							answer3.setWenjuanId(wenjuanId);
							answer3.setQuestionId(questionId);
							answer3.setLabel(xx+"");	
							xx++;
							answer3.setLabelVal(strQuestion3[j]);
							answer3.setRecordTime(date);
							answerService.insertSelective(answer3);
						}
						if(StringUtils.isNotBlank(strQuestion4[j])){
							TbWenjuanAnswer answer4 = new TbWenjuanAnswer();
							answer4.setWenjuanId(wenjuanId);
							answer4.setQuestionId(questionId);
							answer4.setLabel(xx+"");						
							answer4.setLabelVal(strQuestion4[j]);
							answer4.setRecordTime(date);
							answerService.insertSelective(answer4);
						}						
					}
				 			 			 
				
			rtnMap.put("status", "true");
			rtnMap.put("msg", "提交成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "提交失败");
			e.printStackTrace();
		}
		return rtnMap;

	}	
	
}
